import { Component, OnInit } from '@angular/core';
import { ActivatedRoute,Params } from '@angular/router';
@Component({
  selector: 'app-product',
  templateUrl: './product.component.html',
  styleUrls: ['./product.component.css']
})
export class ProductComponent implements OnInit {

  productId:number;
  productName:string;
  constructor(private routeinfo:ActivatedRoute) { }

  ngOnInit() {
    //使用路由快照的方式获取查询传递的参数
    // this.productId = this.routeinfo.snapshot.queryParams['id'];//查询参数传递
    // this.productId = this.routeinfo.snapshot.params['id'];//直接在路由路径中参数传递
    //使用订阅的方式处理参数的获取
    // this.routeinfo.params.subscribe((params:Params) => this.productId = params['id']);
    this.routeinfo.data.subscribe((data:{product:Product}) => {
      this.productId = data.product.id;
      this.productName = data.product.title;
    })
  }

}
export class Product{
  constructor(public id:number,public title:string){

  }
}
